//
//  ViewController.m
//  Main_Oc
//
//  Created by wangbaoping on 2022/4/12.
//

#import "AdVc.h"
#import "api/AdApi.h"
#import "home/HomeFirstVc.h"
#import "common/BaseTbc.h"
#import "PageManager.h"
#import "common/Constant.h"


int count = 3;

@interface AdVc ()

@property(strong,nonatomic) NSTimer * timer;

@property (weak, nonatomic) IBOutlet UIButton *timeButton;

@end

@implementation AdVc

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)timeClick:(UIButton *)sender {
    [self.timer invalidate];
    [self enterGuideOrHome];
}

-(void)updateTime:(NSTimer *)timer{
    self->_timeButton.hidden = false;
    if (count <= 0) {
        [timer invalidate];
        [self enterGuideOrHome];
    }
    self->_timeButton.titleLabel.text = [NSString stringWithFormat:@"%d",count];
    count--;
}

-(void)enterGuideOrHome{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    bool guided = [userDefaults objectForKey:KEY_GUIDE];
    if (guided) {
        [PageManager enterHome:self.navigationController];
    }else{
        [userDefaults setBool:true forKey:KEY_GUIDE];
        [userDefaults synchronize];
        [PageManager enterGuide:self.navigationController];
    }
    
}

#pragma mark 加载数据
- (void)onLoadData{
    [super onLoadData];
    AdApi *api = [[AdApi alloc] init];
    api.apiDalegate = (id)self;
    [api startReqeust];
}

#pragma mark 加载数据成功
- (void)onApiSuccess:(AdModel *)model{
    [super onApiSuccess:model];
    self->_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:)userInfo:@""  repeats:YES];
}

@end



